package com.wulin;
import java.sql.*;

public class JbdcObdcConnection {
    String sDBDriver = null;
    String sConnStr = null;
    // String user = null;
    //String pass = null;
    private static String User = "root";//"test"//"root"
    private static String Pass = "123456";//"test123456"//"123456"
    Connection conn = null;
    Statement stmt = null;


    public JbdcObdcConnection(){
        //sDBDriver="com.mysql.jdbc.Driver";//mysql 5
        sDBDriver="com.mysql.cj.jdbc.Driver";//mysql 8
        //sConnStr="jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=GMT&useUnicode=true&characterEncoding=UTF-8" ;
        sConnStr="jdbc:mysql://localhost:3306/agriculture?useSSL=false&useUnicode=true&characterEncoding=UTF-8" ;
        //sConnStr="jdbc:mysql://129.204.76.69:3306/test?useSSL=false&useUnicode=true&characterEncoding=UTF-8" ;
        //sDBDriver="com.hxtt.sql.access.AccessDriver";
        //sConnStr="jdbc:Access:///d:/Database1.accdb";

        try{
            Class.forName(sDBDriver);

        }
        catch(ClassNotFoundException classnotfoundexception){
            System.err.println("加载驱动程序失败："+classnotfoundexception.getMessage());
        }
    }
    public void getConnection(){
        try{
            conn=DriverManager.getConnection(sConnStr, User, Pass);
            System.out.println("数据库连接成功");
            //conn=DriverManager.getConnection(sConnStr);
            //conn.setAutCommit(false);
            stmt=conn.createStatement();
        }
        catch(SQLException sqlexception){
            System.err.println("获得数据库连接失败："+sqlexception.getMessage());
        }
    }

    public ResultSet executeQuery(String s) {
        try {
            if (conn == null)
                getConnection();
            if (stmt == null)
                stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(s);
            System.out.println("数据库查询成功");
            return rs;

        } catch (SQLException sqlexception) {
            System.err.println("查询失败" + sqlexception.getMessage());
            return null;
        }
    }

    public boolean executeUpdate(String s) {
        try {
            if (conn == null)
                getConnection();
            if (stmt == null)
                stmt = conn.createStatement();
            stmt.executeUpdate(s);
            return true;
        } catch (SQLException sqlexception) {
            System.err.println("更新操作失败" + sqlexception.getMessage());
            return false;
        }
    }

    public boolean updateBatch(String sql[]) {
        try {
            if (conn == null)
                getConnection();
            if (stmt == null)
                stmt = conn.createStatement();
            for (int i = 0; i < sql.length; i++) {
                if (sql[i] != null)
                    stmt.addBatch(sql[i]);
            }
            stmt.executeBatch();
            return true;
        } catch (Exception e) {
            System.out.println("批处理操作失败" + e.toString());
            return false;
        }
    }



    public void commit() {
        try {
            conn.commit();
        } catch (Exception e) {
            System.out.println(e.toString());
        }
    }

    public void close() {
        try {
            if (stmt != null)
                stmt.close();
            if (conn != null)
                conn.close();
        } catch (Exception e) {
            System.out.println(e.toString());
        }
    }
    public PreparedStatement prepareStatement(String s) {
        try {
            if (conn == null)
                getConnection();
            if (stmt == null)
                stmt = conn.createStatement();
            PreparedStatement pstmt=conn.prepareStatement(s);
            return pstmt;
        } catch (SQLException sqlexception) {
            System.err.println("查询失败" + sqlexception.getMessage());
            return null;
        }

    }





}
